package com.lc.boot.app;

import com.lc.boot.context.CommandLineProperties;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import javax.servlet.ServletException;
import java.io.File;

/**
 * @author liuc
 * @date 2021/5/2
 * @description
 **/
public class MyBootApplication {

    public static void run(Class<?> clz, Object[] args){
        //封装启动命令行参数
        CommandLineProperties commandLineProperties = new CommandLineProperties(args);
        try {
            startTomcat(commandLineProperties);
        } catch (LifecycleException | ServletException e) {
            e.printStackTrace();
        }
    }

    private static void startTomcat(CommandLineProperties commandLineProperties) throws LifecycleException, ServletException {
        //启动tomcat
        Tomcat tomcat = new Tomcat();
        Connector connector = new Connector();
        connector.setPort(commandLineProperties.getInteger("server.port",8090));
        connector.setURIEncoding("UTF-8");
        tomcat.getService().addConnector(connector);
        tomcat.addWebapp("/","/Users/liuxiaolu/tomcat-temp");
        tomcat.start();
        tomcat.getServer().await();
    }
}
